
#include  "cdef.h"                                     

/* Foreign headerfiles */
#include"eed_c1.h"
/* Own headerfiles */
#include  "GEAR_C1.H"                          
#include  "GEAR_CT.H"                               
#include  "GEAR_CI.H"                                  

/***************************************************************************
* GLOBAL DEFINITIONS
*
* In this section define
* - all global ROM-constants of your module
* - all global variables of your module
****************************************************************************/

/******************************************************************************
* FILE LOCAL FUNCTION PROTOTYPES
*
* In this section declare
* - all tasks
* - all file local function prototypes needed for your module (static)
******************************************************************************/

/***************************************************************************
* Interface Description: Function called to return the gear position information.
*
*
* Implementation       : Get the gear number that should be displayed.
*
* Return Value         : the gear value.
* 
* Author               : Rao Lei, ID R&D SW WHU
*
****************************************************************************/
GEAR_enGearState GEAR__enGearDisplay( 
	                                   
    uint8 u8DrvModDisp,/*
	*
	*  Description: The value of ShiftLeverPos signal.
	*               
	*  Direction  : in
	*
	*  Values     : 0-7
	*/ 
	uint8 u8TargetGear,/*
	*
	*  Description: The value of TargetGear signal.
	*               
	*  Direction  : in
	*
	*  Values     : 0-15
	*/
	uint8 u8FlashingSignal
	);

/***************************************************************************
* Interface Description: Function called to return if symbols should be 
*                        displayed or not.
*
* Implementation       : Get the gear number that should be displayed.
*
* Return Value         : Gear position.
* 
* Author               : Rao Lei, ID R&D SW WHU
*
****************************************************************************/
GEAR_enSymbolType GEAR__enSymbolDisplay(

    uint8 u8ShiftRecommend_M/*
	*
	* Description  : the shift gear display value
	*
	* Direction  : in
	*
	* Values       : 0 ~ 3
	*                
	*/
	);
/***************************************************************************
* Interface Description: Function called to return if symbols should be
*                        displayed or not.
*
* Implementation       : Get the gear number that should be displayed.
*
* Return Value         : Gear position.
*
* Author               : Rao Lei, ID R&D SW WHU
*
****************************************************************************/
GEAR_enSymbolType GEAR__enGetTgtgear_UpDown(
    uint8 u8Tgtgear_Up, /*
	*
	* Description  : the shift gear up display value
	*
	* Direction  : in
	*
	* Values       : 0 ~ 1
	*                
	*/
    uint8 u8Tgtgear_Down/*
	*
	* Description  : the shift gear down display value
	*
	* Direction  : in
	*
	* Values       : 0 ~ 1
	*                
    */);

void GEAR_vMain(void)
{ 
    SYN_tstGearDpoolData stGearPosi;
    SYN_tstShiftSymbolDpoolData stShiftSymbol;

    /* 从 DPool 获取旧值（避免无效更新） */
    SYN_enGetDpoolGearData(&stGearPosi);
    SYN_enGetDpoolShiftSymbolData(&stShiftSymbol);

    /***************************************************************************
    * 从 CAN 或配置获取信号接口（这里用模拟变量代替，实际应调用信号读接口）
    ***************************************************************************/
    uint8 u8DrvModDisp       = DrvModDisp_read();         /* 档位杆位置 */
    uint8 u8TargetGear       = DisplayGear_read();        /* 目标档位 */
    uint8 u8FlashingSignal   = TCU_gear_flashing_read();  /* 闪烁信号 */

    uint8 u8ShiftRecommend_M = ShiftRecommend_M_read();   /* 换挡推荐 */
    uint8 u8Tgtgear_Up       = Tgtgear_Up_read();         /* 上升挡提示 */
    uint8 u8Tgtgear_Down     = Tgtgear_Down_read();       /* 降挡提示 */

    /***************************************************************************
    * 调用功能接口，计算 Gear 显示值 & Symbol 提示符号
    ***************************************************************************/
    /* 挡位显示 */
    stGearPosi.GEAR_u8GearPosi = 
        (uint8)GEAR__enGearDisplay(u8DrvModDisp, u8TargetGear, u8FlashingSignal);

    /* 优先使用 ShiftRecommend 信号 */
    GEAR_enSymbolType enSymbol = GEAR__enSymbolDisplay(u8ShiftRecommend_M);

    /* 如果 ShiftRecommend 信号无效，则使用 TgtGear_Up/Down 作为备选 */
    if (enSymbol == GEAR__nenSymbolBlank)
    {
        enSymbol = GEAR__enGetTgtgear_UpDown(u8Tgtgear_Up, u8Tgtgear_Down);
    }
    stShiftSymbol.GEAR_u8ShiftSymbolPosi = (uint8)enSymbol;

    /***************************************************************************
    * 更新 DPool 数据
    ***************************************************************************/
    SYN_enSetDpoolGearData(&stGearPosi);
    SYN_enSetDpoolShiftSymbolData(&stShiftSymbol);
}

void GEAR_vInit( void )
{
	/***************************************************************************
	* FUNCTION LOCAL DEFINITIONS
	*
	* In this section define
	* - all function local type definitions
	* - all function local variables (auto and static)
	****************************************************************************/
	SYN_tstGearDpoolData  stGear;
    SYN_tstShiftSymbolDpoolData stShiftSymbol;
    /*
	*
	* Description  : Dpool variable for gear information
	*                
	*
	* Access rights: r/w
	*
	* Values       : Gear Position and GearIndication 
	*/ 
	/***************************************************************************
	* FUNCTION CODE
	****************************************************************************/
	/*Get Dpool Gear information before write into Dpool in order to avoid 
												   unnecessary Dpool updations*/
    SYN_enGetDpoolGearData(&stShiftSymbol);
	SYN_enGetDpoolGearData(&stGear);
	/*Update gear data for DPOOL*/   
	stGear.GEAR_u8GearPosi = GEAR_nGearBlank;
    stShiftSymbol.GEAR_u8ShiftSymbolPosi = GEAR__nenSymbolBlank;
	/*Write into Dpool*/
	SYN_enSetDpoolGearData(&stGear);
    SYN_enSetDpoolGearData(&stShiftSymbol);
}

void GEAR_vDeInit( void )
{
	/***************************************************************************
	* FUNCTION LOCAL DEFINITIONS
	*
	* In this section define
	* - all function local type definitions
	* - all function local variables (auto and static)
	****************************************************************************/
	/***************************************************************************
	* FUNCTION CODE
	****************************************************************************/
	/*Deinitialize gear information*/
	GEAR_vInit();
}

GEAR_enGearState GEAR__enGearDisplay(uint8 u8DrvModDisp, uint8 u8TargetGear, uint8 u8FlashingSignal)
{
	/***************************************************************************
	* FUNCTION LOCAL DEFINITIONS
	*
	* In this section define
	* - all function local type definitions
	* - all function local variables (auto and static)
	****************************************************************************/
	GEAR_enGearState enGearPos;/*
	*
	* Description  : temp variable to store gear position data.
	*                It would be writed to DPool.
	*
	* Access rights: r/w
	*
	* Values       : 0~30
	*/

	/***************************************************************************
	* FUNCTION CODE
	****************************************************************************/
	/**********************************GEAR INFORMATION*********************************
	* TargetGear     TCU_gear_Flash      GearPostion        *
	* 	 D   			 0					  P              *
	* 	 D   			 1			     Flashing P          *
	* 	 1	  			 0    			      D1             *
	*  	 1	  			 1    			 Flashing D1         *
	*	 2	  			 0    			      D2             *
	*	 2	  			 1    			 Flashing D2         *
	*	 3	  			 0    			      D3             *
	* 	 3	  			 1    			 Flashing D3         *
	*	 4	  			 0    			      D4             *
	*	 4	  			 1    			 Flashing D4         *
	*	 5	  			 0    			      D5             *
	*	 5	  			 1    			 Flashing D5         *
	*	 6	  			 0    			      D6             *
	*	 6	  			 1    			 Flashing D6         *
	*	 7	  			 0    			      D7             *
	*	 7	  			 1    			 Flashing D7         *
	*	 8	  			 0    			      D8             *
	*	 8	  			 1    			 Flashing D8         *
	*    0	         	 0					  N              *
	*    0	         	 1				 Falshing N          *
	*    B     			 0		    		  R              *
	*    B      		 1		    	 Falshing R          *
	*    1				 -   				  1              *
	*    2               -                    2              *
	*    3               -                    3              *
	*    4               -                    4              *
	*    5               -                    5              *
	*    6               -                    6              *
	*    7               -                    7              *
	*    8               -                    8              *
	*   Others           -                   Blank           *
    ************************************************************************************/
    switch (u8DrvModDisp)
	{
	case GEAR__nenShiftLeverPosP:        
			if(u8TargetGear == GEAR__nenTargetGearP)
			{	
				enGearPos = ((bool)u8FlashingSignal)?GEAR_nGearFlashingP:GEAR_nGearP;
			}
		    else
			{
				enGearPos = GEAR_nGearBlank; 
			}
			break;
	case GEAR__nenShiftLeverPosD:
			switch( u8TargetGear )
			{
				case GEAR__nenTargetGear1:        enGearPos = ((bool)u8FlashingSignal)?GEAR_nGearFlashingD1:GEAR_nGearD1;        break;
				case GEAR__nenTargetGear2:        enGearPos = ((bool)u8FlashingSignal)?GEAR_nGearFlashingD2:GEAR_nGearD2;        break;
				case GEAR__nenTargetGear3:        enGearPos = ((bool)u8FlashingSignal)?GEAR_nGearFlashingD3:GEAR_nGearD3;        break;
				case GEAR__nenTargetGear4:        enGearPos = ((bool)u8FlashingSignal)?GEAR_nGearFlashingD4:GEAR_nGearD4;        break;
				case GEAR__nenTargetGear5:        enGearPos = ((bool)u8FlashingSignal)?GEAR_nGearFlashingD5:GEAR_nGearD5;        break;
				case GEAR__nenTargetGear6:        enGearPos = ((bool)u8FlashingSignal)?GEAR_nGearFlashingD6:GEAR_nGearD6;        break;
				case GEAR__nenTargetGear7:        enGearPos = ((bool)u8FlashingSignal)?GEAR_nGearFlashingD7:GEAR_nGearD7;        break;
				case GEAR__nenTargetGear8:        enGearPos = ((bool)u8FlashingSignal)?GEAR_nGearFlashingD8:GEAR_nGearD8;        break;
				default:       enGearPos = GEAR_nGearBlank;     break;
			}	
			break;
	case GEAR__nenShiftLeverPosN: 
			if(u8TargetGear == GEAR__nenTargetGearN)
			{
				enGearPos = ((bool)u8FlashingSignal)?GEAR_nGearFlashingN:GEAR_nGearN;
			}
			else
			{
				enGearPos = GEAR_nGearBlank;
			}
			break;
	case GEAR__nenShiftLeverPosR: 
			if(u8TargetGear == GEAR__nenTargetGearR)
			{
				enGearPos = ((bool)u8FlashingSignal)?GEAR_nGearFlashingR:GEAR_nGearR;
			}
			else
			{
				enGearPos = GEAR_nGearBlank;
			}
			break;
	case GEAR__nenShiftLeverPosM:
			switch( u8TargetGear )
			{
			case GEAR__nenTargetGear1:        enGearPos = GEAR_nGear1;         break;
			case GEAR__nenTargetGear2:        enGearPos = GEAR_nGear2;         break;
			case GEAR__nenTargetGear3:        enGearPos = GEAR_nGear3;         break;
			case GEAR__nenTargetGear4:        enGearPos = GEAR_nGear4;         break;
			case GEAR__nenTargetGear5:        enGearPos = GEAR_nGear5;         break;
			case GEAR__nenTargetGear6:        enGearPos = GEAR_nGear6;         break;
			case GEAR__nenTargetGear7:        enGearPos = GEAR_nGear7;         break;
			case GEAR__nenTargetGear8:        enGearPos = GEAR_nGear8;         break;
			default:       enGearPos = GEAR_nGearBlank;     break;
			}
			break;/*Break of case 4 of u8DrvModDisp*/
	default:       
			enGearPos = GEAR_nGearBlank;     
			break;
	}
	return enGearPos;
}

GEAR_enSymbolType GEAR__enSymbolDisplay(uint8 u8ShiftRecommend_M)
{
	/***************************************************************************
	* FUNCTION LOCAL DEFINITIONS
	*
	* In this section define
	* - all function local type definitions
	* - all function local variables (auto and static)
	****************************************************************************/
	GEAR_enSymbolType enReturn;/*
	*
	* Description  : temp variable to store gear symbol data.
	*                It would be writed to DPool.
	*
	* Access rights: r/w
	*
	* Values       : 1-3
	*/
	/***************************************************************************
	* FUNCTION CODE
	****************************************************************************/
	
    if (u8ShiftRecommend_M == GEAR__nenSymbolUp)
	{/*shift up*/
		enReturn = GEAR__nenSymbolUp;
	}
    else if (u8ShiftRecommend_M == GEAR__nenSymbolDown)
	{/*shift down*/
		enReturn = GEAR__nenSymbolDown;
	}
	else
	{/*blank*/
		enReturn = GEAR__nenSymbolBlank;
	}
  
	return enReturn;
}

GEAR_enSymbolType GEAR__enGetTgtgear_UpDown(uint8 u8Tgtgear_Up, uint8 u8Tgtgear_Down)
{
    /***************************************************************************
    * FUNCTION LOCAL DEFINITIONS
    *
    * In this section define
    * - all function local type definitions
    * - all function local variables (auto and static)
    ****************************************************************************/
    GEAR_enSymbolType enReturn; /*
    *
    * Description  : temp variable to store gear symbol data.
    *                It would be writed to DPool.
    *
    * Access rights: r/w
    *
    * Values       : 1-3
    */
    /***************************************************************************
    * FUNCTION CODE
    ****************************************************************************/
    if ((u8Tgtgear_Up == GEAR__nu8Tgtgear_Up_Enable) && (u8Tgtgear_Down == GEAR__nu8Tgtgear_Down_Disable))
    {/*shift up*/
        enReturn = GEAR__nenSymbolUp;
    }
    else if ((u8Tgtgear_Up == GEAR__nu8Tgtgear_Up_Disable) && (u8Tgtgear_Down == GEAR__nu8Tgtgear_Down_Enable))
    {/*shift down*/
        enReturn = GEAR__nenSymbolDown;
    }
    else
    {/*black*/
        enReturn = GEAR__nenSymbolBlank;
    }

    return enReturn;

}

/***************************************************************************
* EOF: GEAR1C1.C
****************************************************************************/
